using Assets.Scripts.System;
using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Search;
using UnityEngine;

public class ObjectPoolSystem : MonoSingleton<ObjectPoolSystem>, IBaseSystem
{
    public Transform tran;
    public List<Pool> poolList;

    private Dictionary<string, ObjectPool> poolDict = new Dictionary<string, ObjectPool>();

    protected override void Awake()
    {
        base.Awake();
    }
    private void Start()
    {
        Init();
    }
    private void Init()
    {
        foreach (Pool pool in poolList)
        {
            pool.poolName = pool.objectPool.poolName;
            poolDict.Add(pool.poolName, pool.objectPool);
            pool.objectPool.Init(tran);
        }
    }
    public GameObject CreateGO(GameObject o,Transform tran)
    {
        GameObject go = Instantiate(o, tran);
        return go;
    }
    public GameObject CreatePoolAndReturnGO(string poolName,string itemName,int queueSize,Transform tran, GameObject go)
    {
        ObjectPool op = null;
        if(poolDict.TryGetValue(poolName,out op))
        {

        }
        else
        {
            poolDict.Add(poolName,ScriptableObject.CreateInstance<ObjectPool>());
            op = poolDict[poolName];
            op.Init(tran);
        }
        return op.CreateObjectPoolItemAndReturnGO(itemName,queueSize,go);
    }
    public GameObject Get(string poolName,string itemName)
    {
        ObjectPool op = null;
        if(poolDict.TryGetValue(poolName, out op))
        {
            GameObject o = op.GetFromPool(itemName);
            o?.GetComponent<IPoolObject>().GetFromPool();
            return o;
        }
        return null;
    }
    public void Put(string poolName, string itemName, GameObject o)
    {
        ObjectPool op = null;
        if (poolDict.TryGetValue(poolName, out op))
        {
            o.GetComponent<IPoolObject>().BackToPool();
            op.BackToPool(itemName,o);
        }
    }

    public void Open()
    {
        throw new NotImplementedException();
    }

    public void Close()
    {
        throw new NotImplementedException();
    }

    public void OnUpdate()
    {
        throw new NotImplementedException();
    }
}
[Serializable]
public class Pool
{
    public string poolName;
    public ObjectPool objectPool;
    public Pool() { }
    public Pool(string pn, ObjectPool objectPool)
    {
        this.poolName = pn;
        this.objectPool = objectPool;
    }
}
